package com.hongsheng.mapper;

/*
@author:刘逸思普
@Date:2023/8/10
 */

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hongsheng.pojo.RA;
import com.hongsheng.pojo.UR;
import com.hongsheng.util.UserSqlProvider;
import com.hongsheng.pojo.User;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface UserMapper extends BaseMapper<User> {

    //查询所有用户的信息
    @SelectProvider(type = UserSqlProvider.class,method = "getUserByCondition")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "name",property = "name"),
            @Result(column = "sex",property = "sex"),
            @Result(column = "phone",property = "phone"),
            @Result(column = "email",property = "email"),
            @Result(column = "address",property = "address"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "last_time",property = "lastTime"),
            @Result(column = "status",property = "status"),
            //配置role
            @Result(
                    javaType = List.class,
                    property = "roles",
                    column = "id",
                    many = @Many(select = "com.hongsheng.mapper.RoleMapper.selectRoleByUserId")
            )
    })
    List<User> selectUser(@Param("name") String name,
                          @Param("sex") Integer sex,
                          @Param("address") String address,
                          @Param("status") Integer status);

    //通过id查询用户信息
    @Select("select * from tb_user where id=#{id}")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "username",property = "username"),
            @Result(column = "password",property = "password"),
            @Result(column = "name",property = "name"),
            @Result(column = "sex",property = "sex"),
            @Result(column = "phone",property = "phone"),
            @Result(column = "email",property = "email"),
            @Result(column = "address",property = "address"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "last_time",property = "lastTime"),
            @Result(column = "status",property = "status"),
            //配置role
            @Result(
                    javaType = List.class,
                    property = "roles",
                    column = "id",
                    many = @Many(select = "com.hongsheng.mapper.RoleMapper.selectRoleByUserId")
            )
    })
    User selectUserById(Integer id);


    @Update("update tb_user set username=#{username}, password=#{password}, name=#{name}, phone=#{phone}, email=#{email}, address=#{address}, sex=#{sex}, status=#{status}  WHERE id=#{id}")
    Boolean updateUser(User user);

    @Insert("insert into tb_user_role (u_id,r_id) values (#{uid},#{rid})")
    Boolean insertUserRole(UR ur);

    @Delete("delete from tb_user_role where u_id=#{uid} and r_id=#{rid}")
    Boolean deleteUserRole(UR ur);

    @Select("select username from tb_user where username=#{username}")
    String selectUsername(String username);

    @Select("select * from tb_user_role where u_id=#{uid} and r_id=#{rid}")
    List<UR> selectUR(UR ur);








}
